<?php

namespace Database\Seeders;

use App\Models\Coupon;
use App\Models\Category;
use Illuminate\Database\Seeder;

class CouponSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 1. 新人专享券（永久有效）
        Coupon::create([
            'name' => '新人专享券',
            'code' => 'NEWUSER50',
            'type' => Coupon::TYPE_FIXED,
            'value' => 50.00,
            'min_purchase' => 200.00,
            'total_quantity' => 1000,
            'per_user_limit' => 1,
            'applicable_type' => Coupon::APPLICABLE_ALL,
            'stackable' => false,
            'description' => '新用户专享，立减50元',
            'usage_note' => '仅限新用户使用，满200元可用',
            'is_active' => true,
            'is_new_user_only' => true,
            'sort_order' => 100,
        ]);

        // 2. 满减券（进行中）
        Coupon::create([
            'name' => '满300减50',
            'code' => 'SAVE50',
            'type' => Coupon::TYPE_FULL_REDUCTION,
            'value' => 50.00,
            'min_purchase' => 300.00,
            'total_quantity' => 500,
            'per_user_limit' => 2,
            'start_time' => '2024-10-01 00:00:00',
            'end_time' => '2024-12-31 23:59:59',
            'applicable_type' => Coupon::APPLICABLE_ALL,
            'stackable' => false,
            'description' => '满300元立减50元',
            'usage_note' => '满300元可用，不可与其他优惠券叠加',
            'is_active' => true,
            'is_new_user_only' => false,
            'sort_order' => 90,
        ]);

        // 3. 折扣券（进行中）
        Coupon::create([
            'name' => '全场9折',
            'code' => 'DISCOUNT10',
            'type' => Coupon::TYPE_PERCENT,
            'value' => 9.0,
            'min_purchase' => 100.00,
            'max_discount' => 200.00,
            'total_quantity' => 300,
            'per_user_limit' => 1,
            'start_time' => '2024-10-01 00:00:00',
            'end_time' => '2024-12-31 23:59:59',
            'applicable_type' => Coupon::APPLICABLE_ALL,
            'stackable' => false,
            'description' => '全场商品9折优惠',
            'usage_note' => '满100元可用，最高优惠200元',
            'is_active' => true,
            'is_new_user_only' => false,
            'sort_order' => 80,
        ]);

        // 4. 分类专用券（手机类）- 进行中
        $phoneCategory = Category::where('name', 'Orange Phone')->first();
        if ($phoneCategory) {
            Coupon::create([
                'name' => '手机专享券',
                'code' => 'PHONE100',
                'type' => Coupon::TYPE_FULL_REDUCTION,
                'value' => 100.00,
                'min_purchase' => 500.00,
                'total_quantity' => 200,
                'per_user_limit' => 1,
                'start_time' => '2024-10-01 00:00:00',
                'end_time' => '2024-12-31 23:59:59',
                'applicable_type' => Coupon::APPLICABLE_CATEGORY,
                'applicable_ids' => [$phoneCategory->id],
                'stackable' => false,
                'description' => '手机类商品专享，满500减100',
                'usage_note' => '仅限手机类商品使用，满500元可用',
                'is_active' => true,
                'is_new_user_only' => false,
                'sort_order' => 70,
            ]);
        }

        // 5. 限时优惠券（3天）
        Coupon::create([
            'name' => '限时特惠券',
            'code' => 'LIMITED20',
            'type' => Coupon::TYPE_FIXED,
            'value' => 20.00,
            'min_purchase' => 50.00,
            'total_quantity' => 100,
            'per_user_limit' => 1,
            'start_time' => now(),
            'end_time' => now()->addDays(3),
            'applicable_type' => Coupon::APPLICABLE_ALL,
            'stackable' => false,
            'description' => '限时3天特惠，立减20元',
            'usage_note' => '限时优惠，满50元可用',
            'is_active' => true,
            'is_new_user_only' => false,
            'sort_order' => 60,
        ]);

        $this->command->info('✅ 创建了 5 个优惠券');
    }
}
